/*
 * CirclePreviewWindowController.java
 *
 * Created on November 4, 2006, 10:55 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.circle;

import edu.umn.cs5115.scheduler.framework.DocumentController;
import edu.umn.cs5115.scheduler.framework.KeyValueAdapter;
import edu.umn.cs5115.scheduler.framework.KeyValueCoding;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver;
import edu.umn.cs5115.scheduler.framework.WindowController;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver.SetMutationType;

import java.util.Set;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;

/**
 *
 * @author grant
 */
public class CirclePreviewWindowController extends WindowController
{
    /** The circle drawer we created. (and will need to explicitly destroy when
     * the window closes - I thought SWT would do this for us, but it apparently
     * does not. */
    private CircleDrawer drawer;
    
    /** Listens to the circle to change the window's title when the circle's name changes. */
    KeyValueObserver circleNameListener = new KeyValueAdapter()
    {
        public void valueChanged(KeyValueCoding object, String key)
        {
            Shell window = getWindow();
            if (window != null)
                window.setText(getWindowTitle());
        }
    };
    
    /** Creates a new instance of CirclePreviewWindowController */
    public CirclePreviewWindowController(CircleDocument document)
    {
        super(document);
        
        Shell window = new Shell(DocumentController.getSharedDocumentController().getDisplay());
        GridLayout gridLayout = new GridLayout(1, true);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        window.setLayout(gridLayout);
        
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        drawer = new CircleDrawer(window, document.getCircle());
        drawer.setLayoutData(data);
        
        document.getCircle().addListener(circleNameListener, Circle.NAME_KEY);
        
        setWindow(window);
    }
    
    protected void disposing()
    {
        drawer.dispose();
        ((CircleDocument)getDocument()).getCircle().removeListener(circleNameListener, Circle.NAME_KEY);
    }
    
    public String getWindowTitle()
    {
        return "Preview of \""+((CircleDocument)getDocument()).getCircle().getName()+"\" in document \"" + getDocument().getDisplayName() + "\"";
    }
}
